package com.hzhr.iot.device.service;

import com.hzhr.iot.collect.sdk.model.collectLog.CollectLogGetModel;
import com.hzhr.iot.collect.sdk.model.collectLog.CollectLogModel;
import com.hzhr.iot.collect.sdk.service.Collect_CollectLogService;
import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.model.concentrator_command.Concentrator_CommandGetModel;
import com.hzhr.iot.device.model.concentrator_command.Concentrator_CommandModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

@Service
@CygService(model = Concentrator_CommandModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
        @CygQuery(name = "_equal_functionCode", query = "equal_functionCode", type = String.class),
        @CygQuery(name = "_equal_starter", query = "equal_starter", type = Integer.class),
        @CygQuery(name = "_equal_type", query = "qual_type", type = Integer.class),
        @CygQuery(name = "_greaterthanorequal_startTime", query = "greaterthanorequal_startTime", type = Date.class),
        @CygQuery(name = "_lessthan_createTime", query = "lessthan_createTime", type = Date.class),
})
public class Concentrator_CommandService extends BaseService {

    @Autowired
    IotDeviceCustomConvertor iotDeviceCustomConvertor;

    @Autowired
    IotDeviceDao iotDeviceDao;

    @Autowired
    Collect_CollectLogService collect_collectLogService;

    //region javadoc注释 : getByConcentratorId
    /**
     * 查询用户所在物联网设备公司下的集中器的指令 <br/><br/>
     * 简介 : 查询用户所在物联网设备公司下的集中器的指令 <br/>
     * 步骤 : <br/>
     * 1. 验证集中器的存在性 : <br/>
     * 如果不存在IotDeviceEntity对象满足以下条件，则抛出异常201： <br/>
     *           IotDeviceEntity.id = 路径参数concentratorId； <br/>
     *           IotDeviceEntity.companyId = 登录用户.companyId； <br/>
     * 2. 查询集中器的指令 : <br/>
     * 根据条件参数查询collect-service.CollectLogModel对象列表collectLogList_1及总数， <br/>
     * 额外查询条件如下： <br/>
     *           CollectLogModel.iotDeviceId = 路径参数concentratorId； <br/>
     * 3. 返回信息 : <br/>
     * 根据条件参数，将collectLogList_1及总数组装成结果对象并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where Concentrator_CommandModel.id = #{_equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_functionCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where Concentrator_CommandModel.functionCode = #{_equal_functionCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_starter : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : 筛选条件 : where Concentrator_CommandModel.starter = #{equal_starter} ;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where Concentrator_CommandModel.type = #{_equal_type};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_startTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where Concentrator_CommandModel.startTime >= #{_greaterthanorequal_startTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _lessthan_startTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where Concentrator_CommandModel.startTime < #{_lessthan_startTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param concentratorId 集中器编号 <br/><br/>
     * @throws ServiceException <br/>
     * CONCENTRATOR_COMMANDSERVICE_GET_BY_CONCENTRATOR_ID_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证集中器的存在性<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 集中器不存在<br/>
     * @see Concentrator_CommandGetModel result <br/>
     */ 
    //endregion
    //region 说明 : getByConcentratorId
    //查询用户所在物联网设备公司下的集中器的指令
    //简介 : 查询用户所在物联网设备公司下的集中器的指令
    //步骤 :
    //1. 验证集中器的存在性 :
    //如果不存在IotDeviceEntity对象满足以下条件，则抛出异常201：
    //          IotDeviceEntity.id = 路径参数concentratorId；
    //          IotDeviceEntity.companyId = 登录用户.companyId；
    //2. 查询集中器的指令 :
    //根据条件参数查询collect-service.CollectLogModel对象列表collectLogList_1及总数，
    //额外查询条件如下：
    //          CollectLogModel.iotDeviceId = 路径参数concentratorId；
    //3. 返回信息 :
    //根据条件参数，将collectLogList_1及总数组装成结果对象并返回；
    //endregion
    //region 参数 : getByConcentratorId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "concentratorId", value = "集中器编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where Concentrator_CommandModel.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_functionCode", value = "筛选条件 : where Concentrator_CommandModel.functionCode = #{equal_functionCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_starter", value = "筛选条件 : w筛选条件 : where Concentrator_CommandModel.starter = #{equal_starter} ",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_type", value = "筛选条件 : where Concentrator_CommandModel.type = #{equal_type}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_startTime", value = "筛选条件 : where Concentrator_CommandModel.startTime >= #{greaterthanorequal_startTime}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_startTime", value = "筛选条件 : where Concentrator_CommandModel.startTime < #{lessthan_startTime}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where Concentrator_CommandModel.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_functionCode", value = "筛选条件 : where Concentrator_CommandModel.functionCode = #{_equal_functionCode}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_starter", value = "筛选条件 : 筛选条件 : where Concentrator_CommandModel.starter = #{equal_starter} ",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_type", value = "筛选条件 : where Concentrator_CommandModel.type = #{_equal_type}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_startTime", value = "筛选条件 : where Concentrator_CommandModel.startTime >= #{_greaterthanorequal_startTime}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_startTime", value = "筛选条件 : where Concentrator_CommandModel.startTime < #{_lessthan_startTime}",required = false, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : getByConcentratorId
    @RequestMapping(value = "/concentrators/{concentratorId}/commands", method = RequestMethod.GET)
    //endregion
    public Concentrator_CommandGetModel getByConcentratorId(Map<String, String> header, Integer concentratorId,
                                                            Map<String, Object> query) throws ServiceException {


       //1. 验证集中器的存在性
        UserCardModel userCard = getUserCard(header);
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(concentratorId);
        queryModel.setEqual_companyId(userCard.getCompanyId());
        queryModel.setEqual_isValid("1");
        queryModel.setShow(true);
        IotDeviceEntity iotDeviceEntity = iotDeviceDao.selectEntity(queryModel);
        if (iotDeviceEntity == null){
            throw new ServiceException(Device_ErrorsEnum.CONCENTRATOR_COMMANDSERVICE_GET_BY_CONCENTRATOR_ID_201);
        }

        //2. 查询集中器的指令
        //根据条件参数查询collect-service.CollectLogModel对象列表collectLogList_1及总数，

        query.put("_equal_iotDeviceId",concentratorId);
        CollectLogGetModel collectLogGetModel = collect_collectLogService.get(initHeader(header, AccessModeEnum.GLOBAL),query);
        Concentrator_CommandGetModel result = HzhrJsonUtil.convertObjectType(collectLogGetModel,Concentrator_CommandGetModel.class);
        return result;
    }

}

